package com.frame.pageform.component.validata;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ValidateNumberServlet extends HttpServlet{

	public void init() throws ServletException
	{
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws
			ServletException, IOException
	{
		process(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws
			ServletException, IOException
	{
		process(request, response);
	}
	private void process(HttpServletRequest request,
						 HttpServletResponse response) throws ServletException, IOException
	{
		String sizestr=request.getParameter("size");
		String typestr=request.getParameter("type");
		String bgColor=request.getParameter("bgColor");
		String fontColor=request.getParameter("fontColor");
		int size=4,type=0;
		if(sizestr!=null && !"".equals(sizestr))
			size=Integer.parseInt(sizestr);
		if(typestr!=null && !"".equals(typestr))
			type=Integer.parseInt(typestr);
		response.setContentType("image/jpeg");
		response.setHeader("Pragma","No-cache");
		response.setHeader("Cache-Control","no-cache");
		response.setDateHeader("Expires", 0);
		ValidateNumber.setGenerator(new ValidateNumberGenerator(size,type,bgColor,fontColor));
		BufferedImage img=ValidateNumber.createImgNumber(request.getSession());
		ImageIO.write(img, "jpg", response.getOutputStream());
	}
}
